package com.xilihui.xlh.business.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.ChecklistActivity;
import com.xilihui.xlh.business.activitys.ConfirmListingActivity;
import com.xilihui.xlh.business.entities.BalanceEntity;
import com.xilihui.xlh.business.entities.GoodDetailsEntity;
import com.xilihui.xlh.business.entities.SpecEntity;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.ScreenUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/5/30. 
 * 仓库领货 领取
 */

public class BuyDialog implements View.OnClickListener {
    
    private Activity activity;
    private Dialog dialog;
    private TextView tv_price;
    private TextView tv_count;
    private TextView tv_num;
    
    int num = 0;
    String goods_id = "";
    String item_id = "";
    String id = "";
    String wa_id = "";
    
    ArrayList<GoodDetailsEntity.FilterSpecBean> data;
    String[] select;
    
    
    
    GoodDetailsEntity detailsEntity;
    
    public BuyDialog(Activity activity,GoodDetailsEntity goodDetailsEntity,String id,String wa_id){
        this.activity = activity;
        this.detailsEntity = goodDetailsEntity;
        this.id = id;
        item_id = detailsEntity.getItem_id();
        this.wa_id = wa_id;
        goods_id = detailsEntity.getGoods().getGoods_id();
        dialog = new Dialog(activity,R.style.Dialog);
        dialog.setContentView(R.layout.dialog_buy);
        dialog.setCancelable(true);
        dialog.getWindow().setGravity(Gravity.BOTTOM);
        dialog.setCanceledOnTouchOutside(true);
        setDialogWidth();
        initView();
    }
    
    
    
    public void initView(){

        RecyclerView recyclerView = (RecyclerView)dialog.findViewById(R.id.recyclerview_jinhanliang);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity,LinearLayout.VERTICAL,false));
                
        data = (ArrayList<GoodDetailsEntity.FilterSpecBean>) detailsEntity.getFilter_spec();
        select = new String[data.size()];
        
        BaseAdapter<GoodDetailsEntity.FilterSpecBean> baseAdapter = new BaseAdapter<GoodDetailsEntity.FilterSpecBean>(activity,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_marque;
            }

            @Override
            protected void getView(BaseViewHolder holder, GoodDetailsEntity.FilterSpecBean entity, final int pos) {
                
                holder.setText(R.id.tv_shuxing,entity.getName());
                RecyclerView recyclerView1 = (RecyclerView)holder.getView(R.id.recyclerview);
                recyclerView1.setLayoutManager(new LinearLayoutManager(activity, LinearLayout.HORIZONTAL,
                        false));
                
                final ArrayList<GoodDetailsEntity.FilterSpecBean.SpecBean> specBeans = (ArrayList<GoodDetailsEntity.FilterSpecBean.SpecBean>) entity.getSpec();
                

                BaseAdapter<GoodDetailsEntity.FilterSpecBean.SpecBean> baseAdapter1 = 
                        new BaseAdapter<GoodDetailsEntity.FilterSpecBean.SpecBean>(activity,specBeans) {
                    @Override
                    protected int setLayoutId() {
                        return R.layout.recycler_item_good_model;
                    }

                    @Override
                    protected void getView(BaseViewHolder holder, final GoodDetailsEntity.FilterSpecBean.SpecBean entity, final int position) {
                        holder.setText(R.id.tv,entity.getItem());
                        if(entity.getSelect().equals("1")){
                            holder.setSelected(R.id.tv,true);
                            select[pos] = specBeans.get(position).getItem_id();
                        }else {
                            holder.setSelected(R.id.tv,false);
                        }
                        
                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                for (int i=0;i<specBeans.size();i++){
                                    if(i==position){
                                        specBeans.get(i).setSelect("1");
                                        select[pos] = specBeans.get(position).getItem_id();
                                        
                                        String key = "";
                                        for (int j=0;j<select.length;j++){
                                            key = key + select[j] +"_";
                                        }
                                        if(!TextUtil.isEmpty(key)){
                                            key = key.substring(0,key.length()-1);
                                        }

                                        MyAppointmentRequest.spce(key,goods_id)
                                                .compose(DoTransform.<SpecEntity>applyScheduler((FragmentActivity) activity,true))
                                                .subscribe(new HttpSubscriber<SpecEntity>(activity) {
                                                    @Override
                                                    protected void onSuccess(SpecEntity entity) {
                                                        tv_count.setText("库存量"+entity.getSpec_goods().getStore_count()+"件");     
                                                        tv_price.setText("￥"+entity.getSpec_goods().getPrice());
                                                        item_id = entity.getItem_id();
                                                    }

                                                    @Override
                                                    protected void onFail(String msg) {
                                                        ToastUtil.toastShortNegative(msg);
                                                    }
                                                });
                                                
                                    }else {
                                        specBeans.get(i).setSelect("0");
                                    }
                                }
                                notifyDataSetChanged();
                            }
                        });
                        
                    }
                };
                baseAdapter1.setAnimationsLocked(true);
                recyclerView1.setAdapter(baseAdapter1);
            }
        };
        recyclerView.setAdapter(baseAdapter);
        tv_num = (TextView)dialog.findViewById(R.id.tv_num);
        dialog.findViewById(R.id.iv_close).setOnClickListener(this);
        dialog.findViewById(R.id.iv_num_jian).setOnClickListener(this);
        dialog.findViewById(R.id.iv_num_jia).setOnClickListener(this);
        dialog.findViewById(R.id.tv_jiesuan).setOnClickListener(this);
        dialog.findViewById(R.id.tv_qingdan).setOnClickListener(this);

        SimpleDraweeView simpleDraweeView = (SimpleDraweeView)dialog.findViewById(R.id.sdv_img);
        simpleDraweeView.setImageURI(UrlConst.baseUrl()+detailsEntity.getGoods().getOriginal_img());
        tv_price = (TextView)dialog.findViewById(R.id.tv_price);
        tv_price.setText("￥"+detailsEntity.getGoods().getShop_price());
        tv_count = (TextView)dialog.findViewById(R.id.tv_kuchuen);
        tv_count.setText("库存量"+detailsEntity.getGoods().getStore_count()+"件");
        
        
    }

    public void setDialogWidth(){
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = ScreenUtil.getScreenWidth(activity);
        dialog.getWindow().setAttributes(lp);
    }
    
    public void show(){
        if(dialog!=null){
            dialog.show();
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_close:
                dialog.dismiss();
                break;
            case R.id.iv_num_jian:
                if(num>0){
                    num = num -1;
                }
                tv_num.setText(num+"");
                break;
            case R.id.iv_num_jia:
                num = num +1;
                tv_num.setText(num+"");
                break;
            case R.id.tv_jiesuan:
                
                if(num<1){
                    ToastUtil.toastShortNegative("请选择购买数量");
                    return;
                }
                
                MyAppointmentRequest.lijijiesuan(id,goods_id,item_id,num+"")
                                    .compose(DoTransform.<BalanceEntity>applyScheduler((FragmentActivity) activity,true))
                                    .subscribe(new HttpSubscriber<BalanceEntity>(activity) {
                                        @Override
                                        protected void onSuccess(BalanceEntity entity) {
                                            Intent intent = new Intent(activity,ConfirmListingActivity.class);
                                            intent.putExtra("data",entity);
                                            intent.putExtra("wa_id",wa_id);
                                            intent.putExtra("agent_id",id);
                                            intent.putExtra("type",1);
                                            intent.putExtra("goods_id",goods_id);
                                            intent.putExtra("item_id",item_id);
                                            intent.putExtra("num",num);
                                            activity.startActivity(intent);
                                            dialog.dismiss();
                                        }
                                        
                                        @Override
                                        protected void onFail(String msg) {
                                            ToastUtil.toastShortPositive(msg);
                                        }
                                    });
                
                break;
            case R.id.tv_qingdan:
                
                if(num<1){
                    ToastUtil.toastShortNegative("请选择购买数量");
                    return;
                }
                
                MyAppointmentRequest.joinCart(activity,goods_id,num+"",item_id,id,wa_id)
                                    .compose(DoTransform.<BaseEntity>applyScheduler((FragmentActivity) activity,true))
                                    .subscribe(new HttpSubscriber<BaseEntity>(activity) {
                                        @Override
                                        protected void onSuccess(BaseEntity entity) {
                                            YEventBuses.post(new YEventBuses.Event("joinGoods"));
                                            ToastUtil.toastShortPositive(entity.getMsg());
                                            dialog.dismiss();
                                        }

                                        @Override
                                        protected void onFail(String msg) {
                                            ToastUtil.toastShortNegative(msg);
                                        }
                                    });
                
                break;
        }
    }
}
